package processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import utils.BaseClassScanUtils;

import java.util.Map;

public class MyComponentBeanFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        //通过扫描工具去扫描指定包及其子包下的的所有类
        Map<String, Class> myComponentAnnotation =BaseClassScanUtils.scanMyComponentAnnotation("beans");
        //遍历Map，组装BeanDefinition注册
        myComponentAnnotation.forEach((beanname,clazz)->{
            //获取Beanclassname
            String name = clazz.getName();//bean.OtherBean

            //创建BeanDefinition
            BeanDefinition beanDefinition=new RootBeanDefinition();
            beanDefinition.setBeanClassName(name);
            beanDefinitionRegistry.registerBeanDefinition(name,beanDefinition);
        });
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }
}
